package com.hutra.utils {
	
	/**
	 * http://stevenlevithan.com/demo/parseuri/js/
	 * @author hungconcon
	 */
	public class PURI {
		
		public var source:*;
		public var protocol:*;
		public var authority:*;
		public var userInfo:*;
		public var user:*;
		public var password:*;
		public var host:*;
		public var port:*;
		public var relative:*;
		public var path:*;
		public var directory:*;
		public var file:*;
		public var query:*;
		public var anchor:*;
		public var queryKey:*;
		
		protected var k:Array = [
			"source", 
			"protocol", 
			"authority", 
			"userInfo", 
			"user", 
			"password", 
			"host", 
			"port", 
			"relative", 
			"path", 
			"directory", 
			"file", 
			"query", 
			"anchor"
			];
		protected var o:Object = {q: {name: "queryKey", parser: /(?:^|&)([^&=]*)=?([^&]*)/g}, parser: {strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};
		
		public function PURI() {
			// Override
			Object.prototype.toString = function():String {
				var o:Array = [];
				for (var a:String in this)
					o.push('"' + a + '":"' + this[a] + '"');
				return "{" + o.join(",") + "}";
			};
		}
		
		/**
		 * Breaking down of the link
		 * @param	url
		 * @param	mode
		 */
		public function parser(url:String = '', mode:Boolean = false):void {
			var m:Object = o.parser[mode ? 'strict' : 'loose'].exec(url);
			var i:int = k.length;
			while (i--)
				this[k[i]] = m[i] || '';
			var t:* = this[o.q.name] = new Object();
			;
			this[k[12]].replace(o.q.parser, function(a:*, b:*, c:*):* {
					if (b) {
						t[b] = c;
					}
				});
		}
		
		public function get querys():String {
			return ((this.query !== '') ? '?' + this.query : '');
		}
	}
}